package com.lzm.ydpt.module.mall.fragment;

import android.view.View;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.ViewPager;

import com.byt.framlib.base.BaseFragment;
import com.byt.framlib.baseadapter.lv.LvCommonAdapter;
import com.byt.framlib.baseadapter.lv.LvViewHolder;
import com.byt.framlib.basemvp.BasePresenter;
import com.byt.framlib.commonutils.image.ImageLoaderUtils;
import com.byt.framlib.commonwidget.NoScrollGridView;
import com.byt.framlib.commonwidget.OnNoDoubleClickListener;
import com.byt.framlib.commonwidget.ViewPagerFixed;
import com.byt.framlib.commonwidget.bannerview.BannerView;
import com.byt.framlib.commonwidget.bannerview.loader.MyImageLoader;
import com.byt.framlib.commonwidget.onclicktwo.AntiShake;
import com.byt.framlib.commonwidget.refresh.RefreshHeaderView;
import com.lzm.ydpt.R;
import com.lzm.ydpt.entity.mall.MallCaty;
import com.lzm.ydpt.entity.mall.ProductBean;
import com.lzm.ydpt.module.mall.activity.ProductSearchActivity;
import com.lzm.ydpt.module.mall.adapter.HorizontalGvAdapter;
import com.lzm.ydpt.module.mall.adapter.HorizontalGvPagerAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 商城首页
 */
public class MallHomeFragment extends BaseFragment {
    @BindView(R.id.srf_mall_home)
    SmartRefreshLayout srf_mall_home;
    @BindView(R.id.bvp_mall_home)
    BannerView bvp_mall_home;
    @BindView(R.id.vp_mall_show_caty)
    ViewPagerFixed vp_mall_show_caty;
    @BindView(R.id.ll_mall_caty_dot)
    LinearLayout ll_mall_caty_dot;
    @BindView(R.id.nsgv_mall_goods)
    NoScrollGridView nsgv_mall_goods;
    private static MallHomeFragment sFragment;
    private List<MallCaty> mallCatyList = new ArrayList<>();
    private List<ProductBean> mallProducts = new ArrayList<>();
    private LvCommonAdapter<ProductBean> adapter = null;
    // 放圆点的list
    private List<View> dotViewsList;
    private List<GridView> mGridViewList = null;

    public static MallHomeFragment newsInstance() {
        sFragment = new MallHomeFragment();
        return sFragment;
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_mall_home;
    }

    @Override
    public BasePresenter initPresenter() {
        return null;
    }

    @Override
    protected void initView() {
        initRefresh();
//        initHeadImage();
        initCatyViews();
        initAdapter();
    }

    private void initRefresh() {
        setRefreshLayout(srf_mall_home);
        srf_mall_home.setEnableLoadMore(false);
        srf_mall_home.setEnableRefresh(true);
        srf_mall_home.setRefreshHeader(new RefreshHeaderView(context).getHeaderStyleStaffMain());
        srf_mall_home.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {

            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {

            }
        });
    }

    // banner
    private void initHeadImage(String[] banners) {
        ArrayList<String> pic = new ArrayList<>();
        for (String banner : banners) {
            pic.add(banner);
        }
        bvp_mall_home.setImages(pic)
                .setDelayTime(4000)
                .setImageLoader(new MyImageLoader(R.drawable.rect_bg_white))
                .setOnBannerListener(position -> {
                    /**实现点击事件*/
                    showShortToast("点击的是" + position);
                }).start();
    }

    /**
     * 分类
     */
    private void initCatyViews() {
        for (int i = 0; i < 15; i++) {
            mallCatyList.add(new MallCaty("林棕梦" + i));
        }
        int dataSize = mallCatyList.size();
        // （需要页数 = 总数量 ÷ 每页显示数量）向上取整数
        int PageCount = (int) Math.ceil(dataSize / 10);//

        mGridViewList = new ArrayList<>();
        for (int i = 0; i <= PageCount; i++) {
            GridView appPage = new GridView(context);
            appPage.setAdapter(new HorizontalGvAdapter(context, mallCatyList, i));
            appPage.setNumColumns(5);
            appPage.setVerticalSpacing((int) getResources().getDimension(R.dimen.x30));
            appPage.setHorizontalScrollBarEnabled(false);
            appPage.setVerticalScrollBarEnabled(false);
            mGridViewList.add(appPage);
        }

        if (dataSize % 10 == 0) {
            mGridViewList.remove(mGridViewList.size() - 1);
            PageCount--;
        }

        HorizontalGvPagerAdapter mGvPagerAdapter = new HorizontalGvPagerAdapter(mGridViewList);
        vp_mall_show_caty.setAdapter(mGvPagerAdapter);
        vp_mall_show_caty.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                setDotShow(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        addDot(PageCount);
    }


    /**
     * 创建指定数量的圆点
     *
     * @param dotNumber viewPager的数量
     */
    private void addDot(int dotNumber) {
        if (null == dotViewsList) {
            dotViewsList = new ArrayList<View>();
        }

        for (int i = 0; i <= dotNumber; i++) {
            ImageView dotView = new ImageView(context);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    FrameLayout.LayoutParams.WRAP_CONTENT,
                    FrameLayout.LayoutParams.WRAP_CONTENT);
            dotView.setImageResource(R.drawable.dot_mall_caty_selector);
            // 圆点与圆点之间的距离
            params.leftMargin = 10;
            params.rightMargin = 10;

            // 圆点的大小
            params.height = 15;
            params.width = 15;

            ll_mall_caty_dot.addView(dotView, params);
            dotViewsList.add(dotView);
        }
        // 设置圆点默认选中第一个
        setDotShow(0);
    }

    /**
     * 显示底部圆点导航
     *
     * @param position 选中哪个圆点
     */
    private void setDotShow(int position) {
        if (dotViewsList == null) {
            return;
        }
        for (int i = 0; i < dotViewsList.size(); i++) {
            if (i == position) {
                dotViewsList.get(position).setSelected(true);
            } else {
                dotViewsList.get(i).setSelected(false);
            }
        }
    }

    /**
     * 商品
     */
    private void initAdapter() {
        adapter = new LvCommonAdapter<ProductBean>(context, mallProducts, R.layout.item_mall_home_product_gv) {
            @Override
            protected void convert(LvViewHolder viewHolder, ProductBean item, int position) {
                ImageLoaderUtils.display(viewHolder.getView(R.id.img_show_rc_conner), item.getImage_src());
                viewHolder.setText(R.id.tv_product_name_home, item.getStore_name() + item.getTitle());
                viewHolder.setText(R.id.tv_product_coupon, "");
                viewHolder.setText(R.id.tv_product_recurrence, "");
                viewHolder.setText(R.id.tv_money_amout, "89");
                viewHolder.setOnClickListener(R.id.tv_show_look_same, new OnNoDoubleClickListener() {
                    @Override
                    protected void onNoDoubleClick(View v) {
                        showShortToast("看相似");
                    }
                });
                viewHolder.getConvertView().setOnClickListener(new OnNoDoubleClickListener() {
                    @Override
                    protected void onNoDoubleClick(View v) {
                        showShortToast("详情");
                    }
                });
            }
        };
        nsgv_mall_goods.setAdapter(adapter);
    }

    @OnClick({R.id.tv_sear_data, R.id.img_mall_sear})
    public void onClick(View view) {
        if (AntiShake.check(view.getId())) {
            return;
        }
        switch (view.getId()) {
            case R.id.tv_sear_data:
            case R.id.img_mall_sear:
                openActivity(ProductSearchActivity.class);
                break;
        }

    }

    @Override
    public void onDestroyView() {
        sFragment = null;
        super.onDestroyView();
    }
}